package com.fyzk.activity.lottery.jc.beidan;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.fyzk.activity.app.AppToast;
import com.fyzk.activity.lottery.jc.football.newJz.data.QQListView;
import com.tyjc.lottery.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by fanxs11 on 16/4/26.
 */
public class BeiDanAdapter extends BaseExpandableListAdapter implements QQListView.QQHeaderAdapter{

    protected List<BeiDanBean> infos = new ArrayList<>();
    protected Context context;
    private QQListView listView;

    protected OnBeiDanCheckBallListener listener;
    protected int changCi = 0;
    protected final int maxChangCi = 15;

    public BeiDanAdapter(Context context, List<BeiDanBean> infos, QQListView listView, OnBeiDanCheckBallListener listener){
        this.context = context;
        this.infos = infos;
        this.listView = listView;
        this.listener = listener;
    }

    @Override
    public int getGroupCount() {
        return infos.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return infos.get(groupPosition);
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder groupViewHolder;
        if(convertView==null){
            groupViewHolder = new GroupViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.beidan_list_group, null);
            groupViewHolder.groupName_tv = convertView.findViewById(R.id.beidan_list_group_tv);
            groupViewHolder.jiantou_img = convertView.findViewById(R.id.beidan_list_group_img);

            convertView.setTag(groupViewHolder);
        } else {
            groupViewHolder = (GroupViewHolder) convertView.getTag();
        }

        groupViewHolder.groupName_tv.setText(infos.get(groupPosition).matchTime + " 共"+infos.get(groupPosition).childBeen.size()+"场");
        if (isExpanded) {
            groupViewHolder.jiantou_img.setImageResource(R.drawable.jc_jiantou_down);
        }
        else{
            groupViewHolder.jiantou_img.setImageResource(R.drawable.jc_jiantou_up);
        }
        return convertView;
    }

    private class GroupViewHolder {
        TextView groupName_tv;
        ImageView jiantou_img;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        if (infos.size() > 0 && infos.get(groupPosition).childBeen.size() > 0) {
            return infos.get(groupPosition).childBeen.size();
        } else {
            return 0;
        }
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return infos.get(groupPosition).childBeen.get(childPosition);
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }


    // ===========
    @Override
    public int getQQHeaderState(int groupPosition, int childPosition) {
        final int childCount = getChildrenCount(groupPosition);
        if(childPosition == childCount - 1){
            return PINNED_HEADER_PUSHED_UP;
        }
        else if(childPosition == -1 && !listView.isGroupExpanded(groupPosition)){
            return PINNED_HEADER_GONE;
        }
        else{
            return PINNED_HEADER_VISIBLE;
        }
    }

    @Override
    public void configureQQHeader(View header, int groupPosition, int childPosition, int alpha) {
        TextView textView = header.findViewById(R.id.beidan_list_group_tv);
        textView.setText(infos.get(groupPosition).matchTime + " 共"+infos.get(groupPosition).childBeen.size()+"场");
    }

    private HashMap<Integer,Integer> groupStatusMap = new HashMap<Integer, Integer>();

    @Override
    public void setGroupClickStatus(int groupPosition, int status) {
        groupStatusMap.put(groupPosition, status);
    }

    @Override
    public int getGroupClickStatus(int groupPosition) {
        if(groupStatusMap.containsKey(groupPosition)){
            return groupStatusMap.get(groupPosition);
        }else{
            return 0;
        }
    }

    public void setInfos(List<BeiDanBean> infos) {
        this.infos = infos;
        this.notifyDataSetChanged();
    }

    protected void jiSuanChangCi() {

    }

    protected boolean isMax(int clickNum, CheckBox checkBox) {

        if (clickNum == 0 && changCi == maxChangCi) {
            AppToast.getToast().show("最多选择"+maxChangCi+"场比赛");
            if (checkBox != null) {
                checkBox.setChecked(false);
            }
            return true;
        } else {
            return false;
        }

    }

}

interface OnBeiDanCheckBallListener {
    public void onCheckBall(int changCi);
    public void onZhanKaiClick(BeiDanChildBean infor);
}
